<?php

error_reporting(E_ALL ^ E_NOTICE);
require(__DIR__ . '/init.php');

$serviceConstNames = array('STEAM_SERVICE_NAME', 'STEAM_SERVICE_HOST', 'STEAM_SERVICE_PORT', 'STEAM_SERVICE_URL');
foreach ($serviceConstNames as $constName) {
    if(false == defined($constName)) {
        die('服务器运行常量"' . $constName . '"没有定义,请先定义!' . PHP_EOL);
    }
}

$listen = array(STEAM_SERVICE_HOST, STEAM_SERVICE_PORT);
define('APP_NAME', STEAM_SERVICE_NAME);
define('APP_LISTEN_HOST', STEAM_SERVICE_HOST);
define('APP_LISTEN_PORT', STEAM_SERVICE_PORT);


defined('APP_LOG_LEVEL')  || define('APP_LOG_LEVEL', \Monolog\Logger::WARNING);
$services = require(__DIR__ . '/services.php');

if(false == $settings) {
    $settings = array(
        'daemonize'=>false,
        'max_request'=>10,
        'task_max_request'=>10,
        'worker_num'=>2,
        'task_worker_num'=>2
    );
}
$server = new \MicroService\Server($listen, $services, $settings);
$logger = new \MicroService\Logger();
$logger->addLogHandler(new \Monolog\Handler\StreamHandler('php://output', APP_LOG_LEVEL));

$server->setLogger($logger);
//$classLoader对象在COMMON_INCLUDE_DIR/Config/initialize.php文件中定义
$server->setClassLoader($classLoader);

$server->setBeforeInvoke(array(\Libs\Library\Common\MicroService\Context::class, 'onBeforeInvoke'));
$server->setAfterInvoke(array(\Libs\Library\Common\MicroService\Context::class, 'onAfterInvoke'));
$server->setErrorHandler(array(\Libs\Library\Common\MicroService\Context::class, 'onSendError'));
\Libs\Library\Common\AccessToken::setUserInfoCallback(array(\Libs\Library\User\BaseUser::class, 'getUserInfoByToken'));
#$server->setCrontabService(__DIR__ . '/crontab.php');
$server->start();